<!DOCTYPE html>
<?php
    $frist=!count($_POST);
?>
<html>
    <head>
        <meta charset="UTF-8">
        <title>注册页面</title>
    </head>
    <body>
        <?php
        if($frist){
           
         ?>
         <form method="post" action="register.php" id="form2">
            <p>用户名<input type="text" name="username" id="username"> </p>
            <p>密&nbsp; 码 <input type="password" name="password" id="password"></p>
            <p>手机号<input type="text" name="phone" id="phone"> </p>
            <p>邮&nbsp; 箱<input type="text" name="email" id="email"> </p>
            <input type="submit" value="提交" id="btn">
        </form>
        <?php
        }else{
            header("content-type:html;charset=utf-8");
            $hostname="127.0.0.1";
            $username="root";
            $password="123456";
            $database="add";
            $link=mysqli_connect($hostname, $username,
                $password, $database);
            mysqli_set_charset($link, "utf-8");
            $username = $_POST["username"];
            $password = $_POST["password"];
            $phone = $_POST["phone"];
            $email = $_POST["email"];
            $sql = "select *from user where username='$username'";
            $res = mysqli_query($link,$sql);
            $arr = mysqli_fetch_assoc($res);
            if($arr){
                echo "<script>alert('用户名已存在，重新注册');window.location.href='register.php'</script>";
            }else{
              $sql="INSERT INTO `add`.`user` (`username`, `passwd`, `phone`, `email`)"
                    . " VALUES ('${username}', '${password}', '${phone}', '${email}');";
              $res = mysqli_query($link,$sql);
                if($res){
                    echo "<script>alert('注册成功');window.location.href='login.php';</script>";
                }else{
                    echo "<script>alert('注册失败');</script>";
                }
            }
        }
        
        ?>
           
        
       
           
        
    </body>
</html>
